<?php

class locations_IndexController extends Zend_Controller_Action
{

    public function init()
    {
    	defined('BASE_URL')	|| define('BASE_URL', Zend_Controller_Front::getInstance()->getBaseUrl());
    }

    public function indexAction()
    {
    	$db = new Application_Model_DbTable_DbGlobal();
    	$datas = $db->getLocation();
    	$this->view->location = $datas;
    	if($this->getRequest()->isPost()){
    		$data = $this->getRequest()->getPost();
    		if(isset($data['status'])){
    			$ids = $data['status'];
    			$db->updateUnStatus($ids);
    			$this->_redirect("/users/");   			
    		}elseif (isset($data['unstatus'])){
    			$ids = $data['unstatus'];
    			$db->updateStatus($ids);
    			$this->_redirect("/users/");
    		}   		
    		$id = $data['checkBox']; 
    		if(isset($data['update'])&& $id !=""){   			
    			$this->_redirect("/users/index/edit/id/".$id);
    			$row = $db->getUserType($id);
    			$frmuser = new users_Form_FrmUserType();
    			$this->view->frmusers = $frmuser->frmTypeUser($row);
    		}elseif(isset($data['delete'])&& $id !=""){
		    	$db->deleteUser($id);
		    	Application_Form_FrmMessage::message("Ã¡Å¾â‚¬Ã¡Å¾Â¶Ã¡Å¾â€ºÃ¡Å¾Â»Ã¡Å¾â€�Ã¡Å¾â€�Ã¡Å¸â€™Ã¡Å¾Å¡Ã¡Å¾â€”Ã¡Å¸ï¿½Ã¡Å¾â€˜Ã¡Å¾Â¢Ã¡Å¸â€™Ã¡Å¾â€œÃ¡Å¾â‚¬Ã¡Å¾â€�Ã¡Å¸â€™Ã¡Å¾Å¡Ã¡Å¾Â¾Ã¡Å¾â€�Ã¡Å¸â€™Ã¡Å¾Å¡Ã¡Å¾Â¶Ã¡Å¾Å¸Ã¡Å¸â€¹Ã¡Å¾â€�Ã¡Å¾Â¶Ã¡Å¾â€œÃ¡Å¾â€¡Ã¡Å¸â€žÃ¡Å¾â€šÃ¡Å¾â€¡Ã¡Å¸ï¿½Ã¡Å¾â„¢");
		    	Application_Form_FrmMessage::redirectUrl('/users/');
    		}else { $this->_redirect("/users/");}
    		
    	}
	}
	public function addLocationAction(){
		if($this->getRequest()->isPost()){
			$data = $this->getRequest()->getPost();
			$db = new locations_Model_DbTable_DbLocation();
			$db->addLocation($data);
			if(isset($data['save'])){
				Application_Form_FrmMessage::Sucessfull("បញ្ខូលជោគជ័យ",'/locations/add-location');
			}
			if(isset($data['save_close'])){
				Application_Form_FrmMessage::message("Ã¡Å¾â‚¬Ã¡Å¾Â¶Ã¡Å¾â€�Ã¡Å¾â€°Ã¡Å¸â€™Ã¡Å¾â€¦Ã¡Å¾Â¼Ã¡Å¾â€ºÃ¡Å¾â€�Ã¡Å¸â€™Ã¡Å¾Å¡Ã¡Å¾â€”Ã¡Å¸ï¿½Ã¡Å¾â€˜Ã¡Å¾Â¢Ã¡Å¸â€™Ã¡Å¾â€œÃ¡Å¾â‚¬Ã¡Å¾â€�Ã¡Å¸â€™Ã¡Å¾Å¡Ã¡Å¾Â¾Ã¡Å¾â€�Ã¡Å¸â€™Ã¡Å¾Å¡Ã¡Å¾Â¶Ã¡Å¾Å¸Ã¡Å¸â€¹Ã¡Å¾â€�Ã¡Å¾Â¶Ã¡Å¾â€œÃ¡Å¾â€¡Ã¡Å¸â€žÃ¡Å¾â€šÃ¡Å¾â€¡Ã¡Å¸ï¿½Ã¡Å¾â„¢");
				Application_Form_FrmMessage::redirectUrl('/users');
			}
		}
		$form = new locations_Form_FrmLocation();
		$this->view->frmLocation = $form->frmLocation();
	}
	public function editLocationAction()
	{
		$id = $this->getRequest()->getParam('id');
		$this->view->ids = $id;
		$db = new Application_Model_DbTable_DbGlobal();
		$row = $db->getLocationByID($id);		
		$dbs= new locations_Model_DbTable_DbLocation();
		if($this->getRequest()->isPost()){
			$data = $this->getRequest()->getPost();
			$dbs->editLocation($data);
			if(isset($data['save'])){	
				Application_Form_FrmMessage::Sucessfull("បញ្ចូលជោគជ័យ", "/locations/index/edit-location/id/$id");
				Application_Form_FrmMessage::redirectUrl("/locations/index/edit-location/id/$id");
			}
			if(isset($data['save_add'])){
				Application_Form_FrmMessage::Sucessfull("បញ្ចូលជោគជ័យ", "/locations/index/add-location");
				Application_Form_FrmMessage::redirectUrl("/locations/index/add-location");
			}
			if(isset($data['save_close'])){
				Application_Form_FrmMessage::Sucessfull("បញ្ចូលជោគជ័យ", "/locations/index");
				Application_Form_FrmMessage::redirectUrl("/locations/index");
			}
		}
		$form = new locations_Form_FrmLocation();
		$this->view->frmLocation = $form->frmLocation($row);
	}	 
	public function deleteLocationAction(){
		$id = $this->getRequest()->getParam('id');
		$db = new locations_Model_DbTable_DbLocation();
		$db->deleteLocation($id);
		Application_Form_FrmMessage::message("ការលុបទទួលបានជោគជ័យ!");
		$this->_redirect('/locations/index');
	}
	
	public function getfillteraddAction(){
		if($this->getRequest()->IsPost()){
			$data = $this->getRequest()->getPost();
			$username = $data['username'];
			$sql= "SELECT username FROM tbwu_acl_user WHERE username = '$username'";
			$db = new Application_Model_DbTable_DbGlobal();
			$row = $db->getGlobalDbRow($sql);
			echo Zend_Json::encode($row);
			exit();
		}
	}
}

